const { readVersionRecord } = require('../conf/versionRecord');

// 创建readline对象，监听控制台输入
const readline = require('../../common/inputListener');

module.exports=async function({version,mode}){

  const records = readVersionRecord(mode) || [];

  let flag = false;

  for(let item of records){

    if(item === version){

      flag = true;

      break

    }

  }

  if(flag){

    await isContinueQuestion().then().catch(()=>{

      process.exit(0);

    })

  }

  function isContinueQuestion(){

    return new Promise((resolve,reject)=>{

      const rl = readline.init();
      
      function processAnswer(answer){

        if(answer === 'quit') {

          rl.close();

          reject();

          return

        };

        if(answer === 'yes'){
          
          rl.close();

          resolve();

        }else{

          console.log('请输入yes继续，quit退出程序');

        }

      }

      rl.question('检测到版本号'+version+'冲突，输入yes继续旧文件将被覆盖，quit退出打包程序:',answer=>{

        processAnswer(answer);

      });

      rl.on('line',input=>{

        processAnswer(input);

      });

    });

  }

}